package com.qjcc.ui;

import android.app.Activity;
import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.util.Log;

import com.qjcc.R;
import com.qjcc.app.AppContext;
import com.qjcc.util.HttpAccessUtils;
import com.qjcc.util.SessionUtil;
import com.qjcc.util.SharedPreferencesUtil;
import com.qjcc.util.UIHelper;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.EActivity;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;


@EActivity(R.layout.activity_relogin)
public class ReLoginActivity extends Activity {

    private ReLoginActivity mThis;

    @AfterViews
    void reLogin() {
        mThis = this;
        Map users = SharedPreferencesUtil.getLoginUserMap(ReLoginActivity.this);
        //开始重新登陆
        String url = this.getResources().getString(R.string.loginUrl);
        final Map loginMap = new HashMap();
        loginMap.put("loginName", users.get("loginName"));
        loginMap.put("password", users.get("password"));
        loginMap.put("appType", "android");
        loginMap.put("channelid", null);
        loginMap.put("userId", null);
        HttpAccessUtils.callHttpAccess(this, url, loginMap, new HttpAccessUtils.HttpAccessCallBack() {
            @Override
            public void callBack(Object result) {
                try {
                    Log.i("result", result.toString());
                    JSONObject resultJson = new JSONObject(result.toString());
                    String msg = resultJson.getString("msg");
                    if (resultJson.getBoolean("ok")) {
                        JSONObject user = resultJson.getJSONObject("user");
                        Map userMap = new HashMap();
                        userMap.put("userId", user.optString("userId", null));
                        userMap.put("userName", user.optString("userName", null));
                        userMap.put("loginName", user.optString("loginName", null));
                        userMap.put("password", loginMap.get("password"));
                        //是否重新登陆
                        userMap.put("isReLogin", "0");

                        SharedPreferencesUtil.saveSessionId(ReLoginActivity.this, "JSESSIONID=" + resultJson.getString("jsessionid"));
                        SharedPreferencesUtil.saveUser(ReLoginActivity.this, userMap);

                        //同步session
                        SessionUtil.synSession(getApplicationContext());
                        Handler mHandler = new Handler() {
                            @Override
                            public void handleMessage(Message msg) {
                                super.handleMessage(msg);
                                toActivity(MainActivity.class);
                            }
                        };

                    } else {
                        UIHelper.ToastMessage(ReLoginActivity.this, msg);
                        SharedPreferencesUtil.removeLoginUserInfo(ReLoginActivity.this);
                        toActivity(LoginActivity.class);
                    }
                } catch (JSONException e) {
                    UIHelper.ToastMessage(ReLoginActivity.this,  AppContext.getAppContext().getResources().getString(R.string.login_failure));
                    e.printStackTrace();
                    SharedPreferencesUtil.removeLoginUserInfo(ReLoginActivity.this);
                    toActivity(LoginActivity.class);
                }
            }
        });


    }

    /**
     * activity跳转
     *
     * @param directClass
     */
    public void toActivity(Class<?> directClass) {
        Intent intent = new Intent();
        intent.setClass(ReLoginActivity.this, directClass);
        startActivity(intent);
        ReLoginActivity.this.finish();
    }
}
